function isEmpty(data) {
    if (!data) {
        return true;
    }
    if (data instanceof Array) {
        return data.length == 0;
    }

    if (data instanceof Object) {
        return Object.keys(data).length == 0;
    }
    return false;
}

function checkFilenameIsOk(JSRuntimeError) {
    const { filename = '', reason = {}, stack = '' } = JSRuntimeError;

    // 桌面UC浏览器注入js报错，外挂js文件
    if (['coupon.js?tms=', 'cwebview::runJS'].some((i) => filename.includes(i))) {
        return false;
    }

    // 必须要包含 /webApp/dist 这些文件
    // 这里有个问题，如果index.php报错了，将无法捕获错误
    const all = [filename, reason.sourceURL, stack].filter((i) => i).join('');
    return all.includes('/webApp/') && all.includes('.js');
}

function WPTCheckIsImpError(data) {
    const { JSRuntimeError } = data || {};
    if (!JSRuntimeError) {
        return false;
    }

    const { message = '' } = JSRuntimeError;

    // 以下路由仅在内部使用，不上报错误
    if (['/live/admin/:uri'].includes(WPT.curRouter)) {
        return false;
    }

    if (
        isEmpty(message) ||
        typeof message !== 'string' || // 消息必须为非空的字符串
        !checkFilenameIsOk(JSRuntimeError)
    ) {
        return false;
    }

    // 错误是string，报错文件名需要包含js， 并且错误不能是以下信息
    return ![
        /^[^A-z\d]/, // 不能以中文开头
        'Script error.',
        'Script error',
        'WeixinJSBridge',
        'Invalid or unexpected token',
        'triggerAppEvent',
        'checkShare is not defined',
        'overflowMap',
        'Minified React error #130',
        'qbSniffJsVersion',
        '_WXJS',
        'regeneratorRuntime',
        'ResizeObserver',
        "Failed to execute 'removeChild'",
        'wxShare.success',
        '__wx._ready',
        'customData',
        'dispatchEvent',
        'invoke',
        'Object._WXJS.init',
        'ruleObj',
        'b is not a function',
        'Uncaught SyntaxError: missing ) after argument list',
        'Maximum call stack size exceeded',
        'cefQuery',
        'toutiao',
        '10_0', // IOS 10.0 有些奇怪的错误 可忽略
        "Cannot read property 'vdata", // videojs错误
        'window.wx.previewImage', // app previewImage不是一个方法
        'mui is not defined', // 日历js没有加载成功
        'The quota has been',
        'QuotaExceededError',
        'DOM Exception 22', // storage溢出
        'A network error',
        "Failed to execute 'send'", // 接口发送失败
        'Unknown error',
        'window.wx.ready', // ready 不是一个方法 待解决
        // 为什么好多页面都会报video.js的错误，需要处理，暂时先忽略掉
        "Cannot read property 'pause' of undefined",
        "Cannot read property 'byteLength' of undefined",
        // promise ignore error
        'loadScript fail',
        'Loading chunk',
        'The element has no supported sources.',
        'Failed to load because no supported source was found.',
        'The request is not allowed',
        'Quota exceeded.',
        'The operation',
        "Cannot read property 'android' of undefined",
        'successPay is not a function',
        'cancelPay is not a function',
        'wxx is not defined',
        "'ActionStatus' of null", // im 内部错误
        'Java exception was raised during method invocation',
        'wxx.wptShare is not a function', // 暂时忽略安卓发拍后的分享错误
        'wxx.', // 暂时忽略安卓wxx报错，需要等待安卓小伙伴修复
        'Java object is gone',
        "document.querySelector('video').setAttribute", // 百度小程序注入js错误
        'on_pop_state_original.apply', // 百度小程序错误
        'document.getElementsByName',
    ].some((i) => {
        if (i instanceof RegExp) {
            return i.test(message);
        }
        return message.includes(i);
    });
}

window.WPTCheckIsImpError = WPTCheckIsImpError;
